﻿Public Class Camera2D
    Protected _zoom As Single
    ' Camera Zoom

    Public _transform As Matrix
    ' Matrix Transform

    Public _pos As Vector2
    ' Camera Position

    Protected _rotation As Single
    ' Camera Rotation

    Public Sub New()
        _zoom = 0.8F
        _rotation = 0.0F
        _pos = Vector2.Zero
    End Sub

    ' Sets and gets zoom
    Public Property Zoom() As Single
        Get
            Return _zoom
        End Get
        Set(value As Single)
            _zoom = value
            If _zoom < 0.1F Then
                _zoom = 0.1F
            End If
        End Set
    End Property
    ' Negative zoom will flip image

    Public Property Rotation() As Single
        Get
            Return _rotation
        End Get
        Set(value As Single)
            _rotation = value
        End Set
    End Property

    ' Auxiliary function to move the camera
    Public Sub Move(amount As Vector2)
        _pos += amount
    End Sub
    ' Get set position
    Public Property Pos() As Vector2
        Get
            Return _pos
        End Get
        Set(value As Vector2)
            _pos = value
        End Set
    End Property

    'Matrix Transformation
    Public Function get_transformation(graphicsDevice As GraphicsDevice) As Matrix
        ' Thanks to o KB o for this solution

        'Translate for basic camera movement
        'Then Add any Rotation that's been applied
        'Then Scale
        'And finally, create it within the screen. [I think]
        _transform = Matrix.CreateTranslation(New Vector3(-_pos.X, -_pos.Y, 0)) * _
            Matrix.CreateRotationZ(Rotation) * _
            Matrix.CreateScale(New Vector3(Zoom, Zoom, 1)) * _
            Matrix.CreateTranslation(New Vector3(graphicsDevice.Viewport.Width * 0.5F, graphicsDevice.Viewport.Height * 0.5F, 0))
        Return _transform
    End Function

End Class
